// const nunjucks = require('nunjucks');

// function creatEnv(path,opts){
//     var
//         autoescape = opts.autoescape === undefined ?true :  opts.autoescape,
//         noCache = opts.noCache ||   false,
//         watch = opts.watch || false,
//         throwOnUndefined = opts.throwOnUndefined || false,
//         env = new nunjucks.Environment(
//             new nunjucks.FileSystemLoader(__dirname + '/views',{
//                 noCache : noCache,
//                 watch : watch,
//             }),{
//                 autoescape : autoescape,
//                 throwOnUndefined : throwOnUndefined
//             });
//     if (opts.filters) {
//         for ( var f in opts.filters){
//             env.addFilter(f, opts.filters[f]);
//         }
//     }
//     return env;
// }

// var env = creatEnv(__dirname + '/views',{
//     watch : true,
//     filters :{
//         hex : function(n){
//             return '0x' + n.toString(16);
//         }
//     }
// });

// // var s = env.render('hello.html',{name:'小明'});
// var s = env.render('hello.html',{name:'<script>alert("小明")</scripit>'});
// //能防止恶意脚本攻击

// console.log(env.render('use-nunjucks/views/base.html',{
//     header:'Hello',
//     body:'blablabla...'
// }));


// console.log(s);

const nunjucks = require('nunjucks');
// const path = require('path');
function createEnv(path, opts) {
    var
        autoescape = opts.autoescape === undefined ? true : opts.autoescape,
        noCache = opts.noCache || false,
        watch = opts.watch || false,
        throwOnUndefined = opts.throwOnUndefined || false,
        env = new nunjucks.Environment(
            new nunjucks.FileSystemLoader(path, {
                noCache: noCache,
                watch: watch,
            }), {
                autoescape: autoescape,
                throwOnUndefined: throwOnUndefined
            });
    if (opts.filters) {
        for (var f in opts.filters) {
            env.addFilter(f, opts.filters[f]);
        }
    }
    return env;
}

var env = createEnv(__dirname + '/views', {
    watch: true,
    filters: {
        hex: function (n) {
            return '0x' + n.toString(16);
        }
    }
});

var s = env.render('hello.html', {
    name: '<Nunjucks>',
    fruits: ['Apple', 'Pear', 'Banana'],
    count: 12000
});

console.log(s);

console.log(env.render('extend.html', {
    header: 'Hello',
    body: 'bla bla bla...'
}));


